<template>
    <el-select v-model="selectedUser" placeholder="请选择用户" clearable @change="handleUserChange">
        <el-option v-for="item in userList" :key="item.userId" :label="item.userName" :value="item.userName">
            <span style="float: left">{{ item.userName }}</span>
            <span style="float: right; color: #8492a6; font-size: 13px">{{ item.nickName }}</span>
        </el-option>
    </el-select>
</template>

<script>
import { listUser } from "@/api/system/user";

export default {
    name: "UserSelect",
    props: {
        value: {
            type: String,
            default: null
        }
    },
    data() {
        return {
            userList: [],
            selectedUser: this.value
        };
    },
    watch: {
        value(val) {
            this.selectedUser = val;
        }
    },
    created() {
        this.getUserList();
    },
    methods: {
        getUserList() {
            listUser().then(response => {
                this.userList = response.rows;
            });
        },
        handleUserChange(value) {
            this.$emit('input', value);
            this.$emit('change', value);
        }
    }
};
</script>